<?php
date_default_timezone_set('Asia/Shanghai');
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2017/11/7
 * Time: 16:03
 */
require './config.php';
//var_dump($_POST);exit;

//$id=$_POST['id'];
$title=htmlentities($_POST['title']);
$name=htmlentities($_POST['name']);
$content=$_POST['content'];
//var_dump($content);exit;
//var_dump($name);exit;
if(empty($title)){
    echo "留言标题不能为空！<br>";
    echo "<meta http-equiv='REFRESH' content='2; url=./index.php'>";
}

if(empty($name)){
    echo "用户名不能为空！<br>";
    echo "<meta http-equiv='REFRESH' content='2; url=./index.php'>";
}

if(empty($content)){
    echo "留言内容不能为空！<br>";
    echo "<meta http-equiv='REFRESH' content='2; url=./index.php'>";
}
//var_dump($_POST);exit;
$fields='';
$values='';
foreach ($_POST as $field=>$value){
    $fields.="`$field`,";
    $values.="'$value',";
}
$fields=rtrim($fields.'`mess_time`',',');
$values=rtrim($values."'".date('Y-m-d H:i:s',time())."'",',');
$sql="insert into lyb ($fields) values($values)";
//$sql="insert into lyb (`id`,`name`,`title`,`content`,`reply`,`state`,`mess_time`) values('null','$name','$title','$content','null','null','null')";
$result = mysqli_query($link,$sql);
if ($result && mysqli_insert_id($link)>0) {
    echo "添加成功";
    echo "<meta http-equiv='REFRESH' content='2; url=./index.php'>";
    exit;
}else{
    echo "添加失败";
    echo "<meta http-equiv='REFRESH' content='2; url=./index.php'>";
    exit;
}